#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <thread>
#include <cstdlib>
using namespace std;

string toHex(pthread_t tid)
{
    char hex[64];
    snprintf(hex,sizeof(hex),"%p",tid);
    return hex;
}

void* Routine(void* arg)
{
	const char* msg = (const char*)arg;
	while (1)
    {
		printf("%s pid: %d, ppid: %d,tid: %lu\n", msg, getpid(), getppid(),pthread_self());
		sleep(1);
	}
}
int main()
{
	pthread_t tid[5];
	for (int i = 0; i < 5; i++)
    {
		char* buf = (char*)malloc(64);
		sprintf(buf, "thread %d", i);
		pthread_create(&tid[i], nullptr, Routine, buf);
        printf("%s tid is %lu\n", buf, tid[i]);
	}
	while (1)
    {
		printf("main thread pid: %d, ppid: %d,tid: %lu\n", getpid(), getppid(),pthread_self());
		sleep(2);
	}
	return 0;
}



/*
//c++11多线程
void threadrun()
{
    while(1)
    {
        cout<<"i am a new thread for C++"<<endl;
        sleep(1);
    }
}

int main()
{
    thread t1(threadrun);
    t1.join();
    return 0;
}
*/

/*原生线程
//传递对象
class request
{
public:
    request(int start,int end,const string &threadname)
    :_start(start)
    ,_end(end)
    ,_threadname(threadname)
    {}
public:
    int _start;
    int _end;
    string _threadname;
};

class response
{
public:
    response(int result,int exitcode)
    :_result(result)
    ,_exitcode(exitcode)
    {}
public:
    int _result;            //计算结果
    int _exitcode;          //计算结果是否可靠
};

void *sumCount(void* args)
{   
    request *rq=static_cast<request*>(args);
    response *rsp=new response(0,0);
    for(int i=rq->_start;i<=rq->_end;i++)
    {
        cout<<rq->_threadname<<" is running, calling..."<<i<<endl;
        rsp->_result+=i;
        usleep(100000);
    }
    delete rq;
    return rsp;
}

int main()
{
    pthread_t tid;
    request *rq=new request(1,100,"thread 1");
    pthread_create(&tid,nullptr,sumCount,rq);

    void *ret;
    pthread_join(tid,&ret);

    response *rsp=static_cast<response*>(ret);
    cout<<"rsp->result: "<<rsp->_result<<",rsp->exitcode: "<<rsp->_exitcode<<endl;

    delete rsp;
    return 0;
}
*/

/*
int g_val=3;
void *Routine(void* args)
{
    const char* name=(const char*)args;
    int cnt=5;
    while(1)
    {
        //cout<<"new thread pid:"<<getpid()<<endl;
        printf("%s, pid: %d,g_val: %d,&g_val: %p\n",name,getpid(),g_val,&g_val);
        sleep(1);
        cnt--;
        if(cnt==0)  break;
    }
    //return nullptr;
    return (void*)1;            //把1返回传给retval
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,Routine,(void*)"thread 1");
   
    
    while(1)
    {
        printf("main thread pid: %d,g_val: %d,&g_val: %p\n",getpid(),g_val,&g_val);
        //cout<<"main thread pid:"<<getpid()<<endl;
        sleep(1);
        g_val;
    }
    

    void* retval;
    pthread_join(tid,&retval);
    cout<<"main thread quit ....,ret: "<<(long long int)retval<<endl;
    return 0;
}
*/

/*
int g_val=3;
void *Routine(void* args)
{
    const char* name=(const char*)args;
    int cnt=5;
    while(1)
    {
        printf("%s, pid: %d,g_val: %d,&g_val: %p\n",name,getpid(),g_val,&g_val);
        sleep(1);
        cnt--;
        if(cnt==0)  break;
    }
    //return nullptr;           //终止线程
    return (void*)1;            //把1返回传给retval
    exit(13);                   //终止进程
    pthread_exit((void*)100);
}


int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,Routine,(void*)"thread 1");
    //return 0;                         //创建新线程，主线程直接退出
    pthread_cancel(tid);                //取消

    void* retval;
    pthread_join(tid,&retval);
    cout<<"main thread quit ....,ret: "<<(long long int)retval<<endl;
    return 0;
}
*/